<?php
namespace app\api\controller;

use app\api\model\Robotnfc as Nfc;
use app\store\model\Device as DeviceModel;

class Base extends Controller
{
    //售货机

    protected $domain = 'https://testopenapi.ksepton.com/open-service'; //星云开物域名
    protected $app_key = 'EP2024241620268';
    protected $app_secret = 'e43f8120a7u94cb4c8515294233a2b12';

    //实例化
    public function obj(){
        return new Nfc();
    }

    //实例化
    public function xykw(){
        return new DeviceModel();
    }

    /**
     * 构建CURL请求
     * @param string $url 请求地址
     * @param int $method 请求方式：0get，1post
     * @param string $sring 请求数据
     */
    public function toCurl($url, $method = 0, $string = '')
    {
        $headers = array(
            'Content-Type: application/json',
        );
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        if ($method) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
        }
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($curl);
        curl_close($curl);
        if (!$output) {
            return array('code'=> -1, 'msg'=>'请求失败');
        }
        $items = json_decode($output,true);
        return array('code' => 0, 'msg' => 'success', 'data' => $items);
    }

    /**
     * 接口签名
     * @param array $params 请求参数
     */
    public function apiSign($params)
    {
        ksort($params);
        $param = json_encode($params);
        $rand = $this->randStr(rand(10, 20));
        $signs = [
            'api_key' => $this->app_key,
            'data' => $param,
            'random_str' => $rand,
            'api_secret' => $this->app_secret,
        ];
        $str = '';
        foreach ($signs as $k => $v) {
            if ($str) {
                $str .= '&'.$k.'='.$v;
            } else {
                $str = $k.'='.$v;
            }
        }
        $str = md5($str);
        return [
            'random_str' => $rand,
            'data' => $params,
            'api_key' => $this->app_key,
            'sign' => $str,
        ];
    }

    /**
     * 生成随机字符串，默认数字字母混合
     * @param int $numeric 0纯数字
     */
    public function randStr($length = 6, $numeric = 1)
    {
        if(!$numeric) {
            //纯数字
            $str = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
        } else {
            //数字、字母组合
            $str = '';
            $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
            $max = strlen($chars) - 1;
            for($i = 0; $i < $length; $i++) {
                $str .= $chars[mt_rand(0, $max)];
            }
        }
        return $str;
    }

    //订单号
    public function orderno(){
        return 'D' . date('YmdHis', time()) . rand(10000, 99999);
    }
}